package com.qingke.action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.qingke.dao.StrutsExampleDAO;
import com.qingke.pojo.User;

public class StreamAction2 extends ActionSupport {
	private InputStream inputStream;
	private String fileName;
	private StrutsExampleDAO dao=new StrutsExampleDAO();
	public InputStream getInputStream() {
		return inputStream;
	}
	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	@Override
	public String execute() throws Exception {
		List<User> list=dao.selectUsers();
		StringBuilder content=new StringBuilder("name,age,height\n");
		for(User user:list){
			content.append(user.getName()+","+user.getAge()+","+user.getHeight()+"\n");
		}
		try{
			inputStream=new ByteArrayInputStream(content.toString().getBytes());
			fileName="download.csv";
		}catch(Exception e){
			return ERROR;
		}
		return SUCCESS;
	}
}
